package logger.sd.server.service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import logger.sd.server.LoggingServer;
import logger.sd.server.MonothreadLoggingServer;
import logger.sd.server.ServerConfiguration;

public class SynchronizeTest {

	protected List<LoggingServer> serverList;
	protected List<ServerConfiguration> configList;
	protected Map<Integer, Integer> ports;

	/**
	 * Construtor
	 */
	public SynchronizeTest() {

		configList = new ArrayList<ServerConfiguration>();
		configList.add(new ServerConfiguration("config01"));
		configList.add(new ServerConfiguration("config02"));
		configList.add(new ServerConfiguration("config03"));

		String[][] argsTemp = {
				{ "7070", "localhost:7071", "log00.log",
						"localhost:7071,localhost:7072", "-first" },
				{ "7071", "localhost:7072", "log01.log",
						"localhost:7070,localhost:7072" },
				{ "7072", "localhost:7070", "log02.log",
						"localhost:7070,localhost:7071" } };

		ports = new HashMap<Integer, Integer>();
		// ports = (from, to)
		ports.put(7070, 7071);
		ports.put(7071, 7072);
		ports.put(7072, 7070);

		serverList = new ArrayList<LoggingServer>();

		for (int i = 0; i < configList.size(); ++i) {
			parse(configList.get(i), argsTemp[i]);
			serverList.add(new MonothreadLoggingServer(configList.get(i)));
		}
	}

	/**
	 * Funcao que le os argumentos da linha de comando.
	 * 
	 * @param args
	 */
	private void parse(ServerConfiguration config, String[] args) {

		config.setFirst(false);
		List<String> servers = new ArrayList<String>();

		config.setPort(args[0]);
		String[] address = args[1].split(":");
		config.setNextHost(address[0]);
		config.setNextPort(address[1]);
		config.setLogfile(args[2]);
		String[] others = args[3].split(":");
		for (int i = 0; i < others.length; ++i) {
			servers.add(others[i]);
		}
		config.setOthersServers(servers);

		if (args.length > 4) {
			config.setFirst(args[4].equals("-first"));
		}
	}

	/**
	 * Inici aos servidores
	 */
	public void runServers() {

		Thread t01 = new Thread(new Runnable() {

			@Override
			public void run() {
				serverList.get(0).start();
			}
		});
		t01.start();

		Thread t02 = new Thread(new Runnable() {

			@Override
			public void run() {
				serverList.get(1).start();
			}
		});
		t02.start();

		Thread t03 = new Thread(new Runnable() {

			@Override
			public void run() {
				serverList.get(2).start();
			}
		});
		t03.start();
	}

	/**
	 * Le um arquivo e coloca numa Lista
	 */
	protected static List<String> readFile(String filename) {
		List<String> ret = new ArrayList<String>();
		try {
			File file = new File(filename);
			FileReader reader = new FileReader(file);
			BufferedReader br = new BufferedReader(reader);
			String request;

			while ((request = br.readLine()) != null) {
				ret.add(request);
			}
			reader.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return ret;
	}

}
